/**
 * 
 */
package gameEngine;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * @author Joe R. Nassimian
 * @author Yannis Mazzer
 */
public class Engine extends SingleFrameApplication{
	private String[] args;
	private Settings gameSettings;
	private int currentPlayerIndex;
	private int nbPlayers;
	private Piece currentPiece;
	private BoardGame boardGame;
	private int turns;
	private boolean turnFinished;
	
	@Override
	protected void initialize(String[] _args) {
		this.args = _args;
		setCurrentPlayerIndex(1);
		setTurns(0);
		setTurnfinished(false);
    }
	
	@Override
	protected void startup() {
		// Debug message
		System.out.println("In Engine.startup() before showing FirstView");
        show(new FirstView(this));
        // Debug message
		System.out.println("In Engine.startup() after showing FirstView and before startGame");
		
		//playGame();
	}

	/**
	 * @return the args
	 */
	public String[] getArgs() {
		return args;
	}
	
	private void initGame(){
		// Debug message
		System.out.println("In Engine.initGame() before creating blokus");
		
		setBoardGame(new Blokus(getGameSettings()));
		
		// Debug message
		System.out.println("In Engine.initGame() after creating blokus");
	}
	
	private void playGame(){
        // Debug message
		System.out.println("Engine.startGame(): Beginning");
		while (!isTurnfinished()){
			
		}
		nextPlayer();
        // Debug message
		System.out.println("Engine.startGame(): Ending");
	}
	
	// Setting up Player management methods
	/**
	 * @param currentPlayer the currentPlayer to set
	 */
	private void setCurrentPlayerIndex(int _currentPlayerIndex) {
		this.currentPlayerIndex = _currentPlayerIndex;
	}

	/**
	 * Switch to next player in game.
	 */
	public void nextPlayer() {
		if(getCurrentPlayerIndex() <= getNbPlayers()){
			this.currentPlayerIndex++;
		} else{
			setCurrentPlayerIndex(1);
		}
		playGame();
	}
	
	/**
	 * @return the currentPlayer's number
	 */
	public int getCurrentPlayerIndex() {
		return currentPlayerIndex;
	}
	
	/**
	 * @return the currentPlayer
	 */
	public Player getCurrentPlayer(){
		return getBoardGame().getPlayer(getCurrentPlayerIndex());
	}
	
	/**
	 * @param nbPlayers the nbPlayers to set
	 */
	private void setNbPlayers() {
		this.nbPlayers = getGameSettings().getNbPlayers();
	}

	/**
	 * @return the nbPlayers
	 */
	public int getNbPlayers() {
		return this.nbPlayers;
	}
	
	/**
	 * @return the players
	 */
	public Player[] getPlayers() {
		return getBoardGame().getPlayers();
	}
	
	/**
	 * @param currentPiece the currentPiece to set
	 */
	public void setCurrentPiece(Piece currentPiece) {
		System.out.println("currentPiece was set");
		this.currentPiece = currentPiece;
	}

	/**
	 * @return the currentPiece
	 */
	public Piece getCurrentPiece() {
		System.out.println("currentPiece was called");
		return currentPiece;
	}
	
	/**
	 * @param turnfinished the turnfinished to set
	 */
	public void setTurnfinished(boolean turnfinished) {
		this.turnFinished = turnfinished;
	}

	/**
	 * @param turns the turns to set
	 */
	public void setTurns(int turns) {
		this.turns = turns;
	}

	/**
	 * @return the turns
	 */
	public int getTurns() {
		return turns;
	}

	/**
	 * @return the turnfinished
	 */
	public boolean isTurnfinished() {
		return turnFinished;
	}

	public String getGameName(){
		return getGameSettings().getGamesName();
	}
	
	/**
	 * @param gameSettings the gameSettings to set
	 */
	public void setGameSettings(Settings _gameSettings) {
		this.gameSettings = _gameSettings;
		setNbPlayers();
		
		// Debug message
		System.out.println("Engine.setGameSettings():"+getGameSettings().toString());
		
		initGame();
	}

	/**
	 * @return the gameSettings
	 */
	public Settings getGameSettings() {
		return gameSettings;
	}

	/**
	 * A Convenient static getter for the application instance.
	 * @return the instance of Engine
	 * */
	public static Engine getApplication(){
		return Application.getInstance(Engine.class);
	}

	/**
	 * @param boardGame the boardGame to set
	 */
	private void setBoardGame(BoardGame boardGame) {
		this.boardGame = boardGame;
	}

	/**
	 * @return the boardGame
	 */
	public BoardGame getBoardGame() {
		return boardGame;
	}
}